package com.prs.crm.dao.ca.work.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.ca.work.InventoryMaintainDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.ca.work.InventoryMaintain;

public class InventoryMaintainDaoImpl extends BaseDaoImpl<InventoryMaintain>
		implements InventoryMaintainDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<InventoryMaintain> getAllInventoryMaintain() {
		return (List<InventoryMaintain>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(InventoryMaintain.class);
				return criteria.list();
			}
		});
	}

	@Override
	public void saveOrInventoryMaintain(InventoryMaintain inventoryMaintain) {
		this.getHibernateTemplate().saveOrUpdate(inventoryMaintain);
	}

	@SuppressWarnings("unchecked")
	@Override
	public InventoryMaintain getInventoryMaintainByArticleTypeId(
			final int articleTypeId) {
		return (InventoryMaintain) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(InventoryMaintain.class);
				criteria.add(Restrictions.eq("articleTypeId", articleTypeId));
				List<InventoryMaintain> inventory = criteria.list();
				if (inventory.size() == 0) {
					return null;
				} else {
					return inventory.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public InventoryMaintain getInventoryMaintainByDate(
			final String imMaintaindate) {
		return (InventoryMaintain) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(InventoryMaintain.class);
				criteria.add(Restrictions.eq("imMaintaindate", imMaintaindate));
				List<InventoryMaintain> inventory = criteria.list();
				if (inventory.size() == 0) {
					return null;
				} else {
					return inventory.get(0);
				}
			}
		});
	}

}
